package com.zh.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.zh.domain.vo.bug.BugVo;
import com.zh.domain.vo.project.ProjectVo;
import com.zh.domain.vo.requirement.RequirementVo;
import com.zh.domain.vo.statistics.StatisticsVo;
import com.zh.domain.vo.task.TaskVo;
import com.zh.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StatisticsServiceImpl implements StatisticsService {

    @Resource
    private ProjectService projectService;

    @Resource
    private RequirementsService requirementsService;
    @Resource
    private TaskService taskService;
    @Resource
    private BugService bugService;
    @Resource
    private UserService userService;

    public StatisticsVo getWeekTaskTrend(Integer projectId) {
        int loginId = StpUtil.getLoginIdAsInt();
        LocalDateTime startOfWeek = LocalDateTime.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).toLocalDate().atStartOfDay();
        LocalDateTime endOfWeek = startOfWeek.plusWeeks(1);
        List<RequirementVo> requirementVos = requirementsService.listByProjectIdAndOwnerId(projectId, loginId);

        List<RequirementVo> filteredRequirementVoList = requirementVos.stream()
                .filter(req -> req.getCompleteTime() != null &&
                        !req.getCompleteTime().isBefore(startOfWeek) &&
                        req.getCompleteTime().isBefore(endOfWeek))
                .collect(Collectors.toList());

        List<TaskVo> taskVoList = taskService.listByProIdAndOwnerId(projectId);
        List<TaskVo> filteredtaskVoList = taskVoList.stream()
                .filter(taskVo -> taskVo.getCompleteTime() != null &&
                        !taskVo.getCompleteTime().isBefore(startOfWeek) &&
                        taskVo.getCompleteTime().isBefore(endOfWeek))
                .collect(Collectors.toList());

        List<BugVo> bugVoList = bugService.listByProIdAndOwnerId(projectId);
        List<BugVo> filteredbugVoList = bugVoList.stream()
                .filter(bugVo -> bugVo.getCompleteTime() != null &&
                        !bugVo.getCompleteTime().isBefore(startOfWeek) &&
                        bugVo.getCompleteTime().isBefore(endOfWeek))
                .collect(Collectors.toList());

        StatisticsVo statisticsVo = new StatisticsVo();
        statisticsVo.setRequirementVoList(filteredRequirementVoList);
        statisticsVo.setTaskVoList(filteredtaskVoList);
        statisticsVo.setBugVoList(filteredbugVoList);
        return statisticsVo;
    }

    @Override
    public StatisticsVo getProjectStatisticsAll(Integer teamId, Integer projectId) {
        int loginId = StpUtil.getLoginIdAsInt();
        List<RequirementVo> requirementVos = requirementsService.listByProjectIdAndOwnerId(projectId, loginId);
        List<TaskVo> taskVoList = taskService.listByProIdAndOwnerId(projectId);
        List<BugVo> bugVoList = bugService.listByProIdAndOwnerId(projectId);
        List<ProjectVo> projectVoList = projectService.listMyTeamProject(teamId, loginId);
        StatisticsVo statisticsVo = new StatisticsVo();
        statisticsVo.setRequirementVoList(requirementVos);
        statisticsVo.setTaskVoList(taskVoList);
        statisticsVo.setBugVoList(bugVoList);
        statisticsVo.setProjectCount(projectVoList.size());
        return statisticsVo;
    }

    @Override
    public StatisticsVo getProjectStatisticsCurrent(Integer projectId) {
        int loginId = StpUtil.getLoginIdAsInt();
        List<RequirementVo> requirementVos = requirementsService.listByProjectIdAndOwnerId(projectId, loginId);
        List<TaskVo> taskVoList = taskService.listByProIdAndOwnerId(projectId);
        List<BugVo> bugVoList = bugService.listByProIdAndOwnerId(projectId);

        List<RequirementVo> filteredRequirementVoList = requirementVos.stream()
                .filter(req -> req.getStatus() != 4)
                .collect(Collectors.toList());
        List<TaskVo> filteredtaskVoList = taskVoList.stream()
                .filter(taskVo -> taskVo.getStatus() != 4)
                .collect(Collectors.toList());
        List<BugVo> filteredbugVoList = bugVoList.stream()
                .filter(bugVo -> bugVo.getStatus() != 4)
                .collect(Collectors.toList());


        StatisticsVo statisticsVo = new StatisticsVo();
        statisticsVo.setRequirementVoList(filteredRequirementVoList);
        statisticsVo.setTaskVoList(filteredtaskVoList);
        statisticsVo.setBugVoList(filteredbugVoList);
        statisticsVo.setProjectCount(1);
        return statisticsVo;
    }

}
